package spring.awareioc;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
import spring.inters.HelloApi;

/**
 * ioc case use ,to know ioc
 */
public class AutowiredBeanTest {
    @Test
    public void autowireByName() throws BeansException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/autowire-byName.xml");
        HelloApi helloApi = context.getBean("bean", HelloApi.class);
        helloApi.sayHello();

    }

    @Test
    public void testImpl() {
        ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/charpter-helloworld.xml");
        HelloApi helloApi = context.getBean("alais1", HelloApi.class);
        helloApi.sayHello();
    }

    /**
     * expected single matching bean but found 3 hello,bean1,bean2
     */
    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/charpter-helloworld.xml");
        HelloApi helloApi = context.getBean(HelloApi.class);
        helloApi.sayHello();
    }

    @Test
    public void testInstantiatingBeanByConstructor() {

        //application context parent interface bean factory
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/charpter-constructor.xml");
        HelloApi bean1 = beanFactory.getBean("bean1", HelloApi.class);
        bean1.sayHello();
        HelloApi bean2 = beanFactory.getBean("bean2", HelloApi.class);
        bean2.sayHello();

    }


    @Test
    public void testInstantiatingBeanByStaticFactory() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/charpter-constructor.xml");
        HelloApi bean1 = beanFactory.getBean("bean3", HelloApi.class);
        bean1.sayHello();

    }

    @Test
    public void testInstantiatingBeanByInstanceFacorty() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/charpter-constructor.xml");
        HelloApi bean4 = beanFactory.getBean("bean4", HelloApi.class);
        bean4.sayHello();

    }


    @Test
    public void testInstantiatingBeanBySetter() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/setterDependencyInject.xml");
        HelloApi bean1 = beanFactory.getBean("bean", HelloApi.class);
        bean1.sayHello();

    }

}
